/**
 * @Author: 安广伟
 * @Email: anguangwei@jwzh.tech
 * @Date: 2024-09-10 10:24:16
 * @LastEditTime 2024-09-10 10:24:16
 * @since: 1.0.0
 * @Version: 1.0.0
 * @Description:
 */

package com.ry.mall.server.controller.wxUser;


import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.ry.mall.server.aspect.watch.TimedOperation;
import com.ry.mall.server.controller.ControllerException;
import com.ry.mall.server.helper.HttpResponseHelper;
import com.ry.mall.server.persistent.entity.WxUserEntity;
import com.ry.mall.server.service.ServiceException;
import com.ry.mall.server.service.wxUser.WxUserService;
import com.ry.mall.server.util.AuthContextHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.annotation.MultipartConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;

@RestController
@MultipartConfig
@RequestMapping("/V1/wxUser")
@Tag(name = "微信用户管理 服务", description = "微信用户管理控制类")
@ApiSupport(author = "徐泽宇 roamerxv@gmail.com", order = 0)
public class WxUserController {

    @Autowired
    private WxUserService wxUserService;

    /**
     * 处理微信用户登录请求
     *
     * @param code 微信小程序提供的授权码
     * @return 包含登录令牌的响应
     * @throws ControllerException 当服务异常时抛出
     */
    @GetMapping("/login")
    @Operation(summary = "微信用户登录", description = "传入code，进行小程序授权登录")
    @TimedOperation(summary = "微信用户登录", description = "传入code，进行小程序授权登录")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "用户登录成功", content = @Content(examples = @ExampleObject(value = ""))),
            @ApiResponse(responseCode = "500", description = "统一类型的错误返回！", content = @Content(examples = @ExampleObject(value = "")))
    })
    public ResponseEntity<String> login(
            @Parameter(required = true, description = "登录凭证code", allowEmptyValue = false, allowReserved = true) @RequestParam(value = "code", required = true) String code)
            throws ControllerException {
        try {
            String token = wxUserService.login(code);
            return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON)
                    .body(HttpResponseHelper.successInfoInbox(token));
        } catch (ServiceException err) {
            throw new ControllerException(err.getMessage());
        }
    }


    /**
     * 获取用户信息接口
     *
     * @return 用户信息封装在ResponseEntity中
     * @throws ControllerException 当服务调用异常时抛出
     */
    @GetMapping("/findUserInfo")
    @Operation(summary = "获取用户信息", description = "传入token，获取用户信息")
    @TimedOperation(summary = "获取用户信息", description = "传入token，获取用户信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询到数据", content = @Content(examples = @ExampleObject(value = ""))),
            @ApiResponse(responseCode = "500", description = "统一类型的错误返回！", content = @Content(examples = @ExampleObject(value = ""))),
            @ApiResponse(responseCode = "508", description = "用户需要重新登录", content = @Content(examples = @ExampleObject(value = "{\n"
                    + //
                    "  \"error\": 508,\n" + //
                    "  \"message\": \"用户未登录,或者超时了。请重新登录！\",\n" + //
                    "  \"time\": \"2024-02-07 20:11:16.811\",\n" + //
                    "  \"trace\": [ ]\n" + //
                    "}")))
    })
    public ResponseEntity<String> findWxUserInfo()
            throws ControllerException {
        try {
            String userId = AuthContextHolder.getUserId();

            WxUserEntity wxUserEntity = wxUserService.findWxUserInfo(userId);
            return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON)
                    .body(HttpResponseHelper.successInfoInbox(wxUserEntity));
        } catch (ServiceException err) {
            throw new ControllerException(err.getMessage());
        }
    }


    /**
     * 更新用户手机号接口
     *
     * 该方法用于传入登录凭证code来更新用户的手机号码
     * 它使用POST请求，并且需要在请求体中包含更新微信用户手机号所需的必要参数
     *
     * @param updateWxPhoneForm 包含用户更新手机号所需数据的表单
     * @return 包含更新结果的响应实体
     * @throws ControllerException 如果服务异常，则抛出控制器异常
     *
     * 注意：此处详细描述了方法的目的和操作流程，以及参数和异常的说明
     */
    @PostMapping("/updateWxUserPhoneNumber")
    @Operation(summary = "更新用户手机号", description = "传入登录凭证code，更新用户手机号")
    @TimedOperation(summary = "更新用户手机号", description = "传入登录凭证code，更新用户手机号")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "更新成功", content = @Content(examples = @ExampleObject(value = ""))),
            @ApiResponse(responseCode = "500", description = "统一类型的错误返回！", content = @Content(examples = @ExampleObject(value = ""))),
            @ApiResponse(responseCode = "508", description = "用户需要重新登录", content = @Content(examples = @ExampleObject(value = "{\n"
                    + //
                    "  \"error\": 508,\n" + //
                    "  \"message\": \"用户未登录,或者超时了。请重新登录！\",\n" + //
                    "  \"time\": \"2024-02-07 20:11:16.811\",\n" + //
                    "  \"trace\": [ ]\n" + //
                    "}")))
    })
    public ResponseEntity<String> updateWxUserPhoneNumber(@RequestBody UpdateWxPhoneForm updateWxPhoneForm)
            throws ControllerException {
        try {
            String code = updateWxPhoneForm.data.code;

            Boolean updateResult = wxUserService.updateWxUserPhoneNumber(code);
            return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON)
                    .body(HttpResponseHelper.successInfoInbox(updateResult));
        } catch (ServiceException err) {
            throw new ControllerException(err.getMessage());
        }
    }
}
